package com.eclipse.training.extensions.preferences;

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;

import com.eclipse.training.extensions.Activator;
import com.eclipse.training.extensions.editors.TrainingPageEditor;

public class PreferenceInitializer extends AbstractPreferenceInitializer {

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#
	 * initializeDefaultPreferences()
	 */
	public void initializeDefaultPreferences() {

		final IPreferenceStore store = Activator.getDefault()
				.getPreferenceStore();

		PreferenceConverter.setDefault(store, IPreferenceConstants.P_COLOR,
				IPreferenceConstants.V_COLOR);

		store.addPropertyChangeListener(new IPropertyChangeListener() {

			@Override
			public void propertyChange(PropertyChangeEvent event) {
				String property = event.getProperty();

				if (property.equals(IPreferenceConstants.P_COLOR)) {

					IEditorPart editor = PlatformUI.getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.getActiveEditor();

					if (editor instanceof TrainingPageEditor)
						((TrainingPageEditor) editor).getViewer().refresh(true);
				}

			}
		});
	}
}
